package cn._2dland.bots.jdo;

import java.util.List;

import javax.jdo.Query;

import cn._2dland.bots.jdo.models.PickRecord;

@SuppressWarnings("unchecked")
public class PickRecordManager extends EntityManager {
	public PickRecordManager() {
		super();
	}

	public void addPickRecord(PickRecord record) {
		pm.makePersistent(record);
	}

	public List<PickRecord> getRecords() {
		Query q = pm.newQuery(PickRecord.class);
		return (List<PickRecord>)q.execute();
	}

	public List<PickRecord> getRecords(Long acctId) {
		Query q = pm.newQuery(PickRecord.class);
		q.setFilter("accountID == " + acctId);
		return (List<PickRecord>)q.execute();
	}

	/**
	 * 存储领取记录
	 * @param acctID - 帐号ID
	 * @param picked - 领取空间大小
	 */
	public static void logPick(Long acctID, int picked){
		PickRecordManager prm = new PickRecordManager();
		try {
			prm.begin();
			PickRecord pr = new PickRecord(acctID, picked, System.currentTimeMillis());
			prm.addPickRecord(pr);
			prm.commit();
		}
		catch (Exception e) {
			prm.rollback();
			e.printStackTrace();
		}
		finally {
			prm.close();
		}
	}
}
